package zhihuilan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import zhihuilan.common.enums.RedisKeyEnum;
import zhihuilan.common.utils.RedisCache;
import zhihuilan.entity.MyLabel;
import zhihuilan.mapper.MyLabelMapper;
import zhihuilan.service.MyLabelService;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 用户数据 服务实现
 *
 * @author ruoyi
 */
@Service
public class MyLabelServiceImpl extends ServiceImpl<MyLabelMapper, MyLabel> implements MyLabelService {

    @Resource
    private RedisCache redisCache;

    @Override
    public List<MyLabel> selectList() {
        QueryWrapper<MyLabel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        List<MyLabel> result = this.baseMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public MyLabel selectByName(String name) {
        String key = RedisKeyEnum.REDIS_KEY_MYLABEL + name;
        MyLabel MyLabel = redisCache.getCacheObject(key);
        if (ObjectUtils.isEmpty(MyLabel)) {
            QueryWrapper<MyLabel> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name", name);
            queryWrapper.eq("status", 1);
            MyLabel = this.baseMapper.selectOne(queryWrapper);
            if (!ObjectUtils.isEmpty(MyLabel)) {
                redisCache.setCacheObject(key, MyLabel, 30, TimeUnit.MINUTES);
            }
        }
        return MyLabel;
    }
}
